//
//  HomeViewController.swift
//  sxwb
//
//  Created by TianDu on 2016/11/11.
//  Copyright © 2016年 TianDu. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController{

    /// UI控件懒加载
    fileprivate lazy var titleBtn : TitleBtn = TitleBtn()
    
    fileprivate lazy var popper : PopperAnimation = PopperAnimation {[weak self] (presented) in
        self?.titleBtn.isSelected = presented
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setNaviItems()
        
        getData()
    }
    
    private func getData(){
        
        RequestManager.share.request(.Post, url: "http://www.dhshenghuo.com/index.php/app/app_custom/login", parameters: ["user_name":"sunshine","password":"111111"], finished: {(result:Any?, error:Error?) -> () in
            
            if error != nil {
                print(error!)
            }else{
                
                print(result!)
            }
        })

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


// MARK: - 设置UI
extension HomeViewController{
    
    fileprivate func setNaviItems() {
        
        if !islogin {
            
            ///设置访客视图
            visitorView.addRotationAnimate()
            let rightBar = UIBarButtonItem(title: "登录", style: .done, target: self, action: #selector(goLogin))
            self.navigationItem.rightBarButtonItem = rightBar
            
            let leftBar = UIBarButtonItem(title: "注册", style: .done, target: self, action: #selector(goRegister))
            self.navigationItem.leftBarButtonItem = leftBar
        }else{
            
            ///登录后的导航栏
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: "navigationbar_friendattention")
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: "navigationbar_pop")
            
            titleBtn.setTitle("Sunshine", for: .normal)
            titleBtn.addTarget(self, action: #selector(titleBtnClick(_:)), for: .touchUpInside)
            self.navigationItem.titleView = titleBtn
        }
    }
}


// MARK: - Btn 点击方法
extension HomeViewController {
    
    override func goLogin() {
        
        let vc = OAuthViewController()
        let nav = WBNavigationController(rootViewController: vc)
        
        present(nav, animated: true, completion: nil)
    }
    
    @objc fileprivate func titleBtnClick(_ btn:TitleBtn) {
        
        let vc = PopperViewController()
        ///设置模态弹出方式为.custom来保证弹出的控制器下层没有被移除
        vc.modalPresentationStyle = .custom
        
        // 设置转成动画的代理及frame
        vc.transitioningDelegate = popper
        popper.presentFrame = CGRect(x: KScreenWidth/4, y: 55, width: KScreenWidth/2, height: KScreenWidth * 1.4/2)
        
        present(vc, animated: true, completion: nil)
        
    }
}

extension HomeViewController{
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

    
    /*
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
     
     // Configure the cell...
     
     return cell
     }
     */
}
